package com.example.student.service.Impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.constants.OtherConstants;
import com.example.common.constants.RdsConstants;
import com.example.common.domain.pojo.Homework;
import com.example.common.domain.pojo.HomeworkStudent;
import com.example.common.utils.CacheClient;
import com.example.common.utils.UUIDUtil;
import com.example.student.mapper.HomeworkMapper;
import com.example.student.mapper.HomeworkStudentMapper;
import com.example.student.service.HomeworkService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class HomeworkServiceImpl extends ServiceImpl<HomeworkMapper, Homework> implements HomeworkService {
    @Autowired
    private CacheClient cacheClient;
    @Autowired
    private HomeworkStudentMapper homeworkStudentMapper;


    @Override
    public Homework getHomeworkByIdWithLogicalExpire(String id) {
        // 使用逻辑过期策略获取教师信息
        return cacheClient.queryWithLogicalExpire(
                RdsConstants.HOMEWORK_INFO,
                id,
                Homework.class,
                this::getById, // 使用MyBatis-Plus的基础方法
                RdsConstants.CACHE_NULL_TTL,
                TimeUnit.MINUTES,
                false
        );
    }

    @Override
    public Boolean submitHomework(String homeworkId, String studentId) {
        HomeworkStudent homeworkStudent = new HomeworkStudent();
        homeworkStudent.setId(UUIDUtil.generate(OtherConstants.ID_LENGTH));
        homeworkStudent.setHomeworkId(homeworkId);
        homeworkStudent.setStudentId(studentId);
        return homeworkStudentMapper.insert(homeworkStudent) > 0;
    }


}
